# 列表推导式
#  生成1-100的数据的列表 lst = [1,2,3,..]
# lst1 = []
#
# for i in range(1,101):
#     lst1.append(i)
# print(lst1)
#
# # [ 值 for 循环 for 循环 if 判断]
# lst2 = [i for i in range(1,101)]  # 一行代码
# print(lst2)

# 将列表中元素包含2个k字符找出来
# lst3 = [["kinig","kinng2","kingk"],["qqkkww","kk22ss","kingwjk"]]

# lst4 = []
# for lst in lst3:
#     for ls in lst:
#         if ls.count("k") == 2:
#             lst4.append(ls)
# # print(lst4)
# lst4 = [ls for lst in lst3 for ls in lst if ls.count("k") == 2]  # 不推荐这种写法，缺少可读性
# print(lst4)

# { k:v for 循环 for 循环 if 判断}
#  字典推导式
# lst = ['king', 'wangyong', 'king152']  # {0:'king',1:'wangyong,2:'king152'}
#
# dic = {i: lst[i] for i in range(len(lst))}
# print(dic)
#
# # 集合推导式
# set1 = {i for i in range(10)}
# print(set1)
#  元组推导式(错误的说法) 新增的方法
# tup = (i for i in range(10))
# print(tup)  # <generator object <genexpr> at 0x000001C4E11FFF68>
# print(tuple(tup))
# a = 123

# 迭代器
# lst = [1,2,3,4,5]

# dir

# 1.只能向后走
# 2.惰性机制
# print("__iter__" in dir(lst))
# it = lst.__iter__()
# # print("__iter__" in dir(it))
# # while 1:
# #     try:
# #         print(it.__next__())  # 去医院看病 叫号 下一个
# #     except StopIteration:
# #         break
# # for i in range(10):
# #     pass

# 生成器 内存

# 使用关键字 yield 来进行修饰  实际上yield与return具备同一个作用，返回值
# 1.减少内存消耗，
# 2.惰性机制
def person(): # 优化内存
    for i in range(10000):
        yield i


num = person()
print(num)
print(num.__next__())
print(num.__next__())
